package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.adapter.BJXAdapter
import com.example.xinhuazidian.adapter.SZJAdapter
import com.example.xinhuazidian.bean.Baijiaxing
import com.example.xinhuazidian.databinding.ActivityBjxactivityBinding
import com.example.xinhuazidian.utils.JsonFileUtil
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 百家姓
 */
class BJXActivity : AppCompatActivity() {

    private lateinit var binding: ActivityBjxactivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBjxactivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        load()
    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        val str = JsonFileUtil.loadFile(this, "baijiaxing.json")

        val bean: Baijiaxing = Gson().fromJson(str, Baijiaxing::class.java)
        initAdapter(bean.paragraphs)
        initAdapter2(bean.origin)

        binding.tvTitle.text = bean.title
        binding.tvAuthor.text = "${bean.tags}·${bean.author}"
    }

    private fun initAdapter(list: MutableList<String>) {
        //设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rv.layoutManager = linearLayoutManager

        //加载适配器
        val szjAdapter = SZJAdapter(list)
        binding.rv.adapter = szjAdapter
    }

    private fun initAdapter2(list: MutableList<Baijiaxing.OriginBean>) {
        val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL)
        binding.rv2.layoutManager = staggeredGridLayoutManager

        //加载适配器
        val bjxAdapter = BJXAdapter(list)
        binding.rv2.adapter = bjxAdapter
    }
}